home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 23 / CU Amiga - Super CD-ROM 23 (June 1998).iso / CUCD / Programming / AMOSList / AMOSLIST / text0336.txt < prev    next >
Encoding:
Text File  |  1998-04-01  |  3.6 KB  |  117 lines

  1.      
  2.      Thanks, I will try this out later.  One thing I should point out is 
  3.      that I made a bit of an error when I said 128 blocks of 256k.  This 
  4.      should read 128 of 256 bytes.  Would this have much effect on the code 
  5.      provided below?
  6.  
  7.  
  8. ______________________________ Reply Separator _________________________________
  9. Subject: Re: Sorting Problem
  10. Author:  Rune Zedeler <rzedeler@post10.tele.dk> at Internet
  11. Date:    27/03/98 20:50
  12.  
  13.  
  14. On 16-Mar-98, Declan_Gorman@modusmedia.com wrote:
  15.      
  16. >     My program has a memory bank which consists of 128 blocks of memory, 
  17. >     each 256k in size.  Each memory block has a name and category and I 
  18. >     want to be able to sort the bank by either.
  19.      
  20. This should do it:
  21.      
  22. <CUT>
  23.      
  24. ' Needs sorting string to be located in S_STR$(0) to S_STR$(NUM-1) 
  25. '   ( ... I don't know how to pass arrays to procs...) 
  26.      
  27. _SORT[128,256*1024,BANK_NUMBER,NUMBER_OF_ITEMS_IN_ARRAY,UNUSED_BANK]
  28.      
  29. Procedure _SORT[NUM,SIZ,BNK,ARR_SIZ,TMP_BNK]
  30.   Shared S_STR$()
  31.      
  32.   Reserve As Work TMP_BNK,SIZ
  33.   Dim CUR(NUM-1),OLD(NUM-1)
  34.      
  35.   'Add number of item to the end of the string 
  36.   For I=0 To NUM-1 : S_STR$(I)=S_STR$(I)+Hex$(I,4) : Next I
  37.   'ensure that unused items in array will be placed first after sorting 
  38.   If ARR_SIZ>NUM
  39.     For I=NUM To ARR_SIZ-1 : S_STR$(I)='' : Next I
  40.   End If 
  41.   Sort S_STR$(0)
  42.   'move sorted items FROM last TO first in array 
  43.   For I=0 To NUM-1 : S_STR$(I)=S_STR$(I+ARR_SIZ-NUM) : Next I
  44.      
  45.   'At this point the first NUM indexes in S_STR$() contains sorted 
  46.   'data - followed by the original position in the array.
  47.      
  48.   For I=0 To NUM-1 : CUR(I)=I : OLD(I)=I : Next I 
  49.   'CUR(ORIGINAL_POS) contains CURRENT_POS  
  50.   'OLD(CURRENT_POS) contains ORIGINAL_POS
  51.      
  52.   For I=0 To NUM-1
  53.     'OLDI=what index index I had before sorting. 
  54.     OLDI=Val(Right$(S_STR$(I),5))
  55.     'J=Current position of block 
  56.     J=CUR(OLDI)
  57.     Print OLDI,J
  58.     S_STR$(I)=Left$(S_STR$(I),Len(S_STR$(I))-5) 
  59.     'swap blocks:
  60.     If I<>J
  61.       Copy Start(BNK)+I*SIZ,Start(BNK)+(I+1)*SIZ To Start(TMP_BNK) 
  62.       Copy Start(BNK)+J*SIZ,Start(BNK)+(J+1)*SIZ To Start(BNK)+I*SIZ 
  63.       Copy Start(TMP_BNK),Start(TMP_BNK)+SIZ To Start(BNK)+J*SIZ 
  64.       Swap CUR(OLD(I)),CUR(OLD(J))
  65.       Swap OLD(I),OLD(J)
  66.     End If 
  67.   Next I
  68.   Erase TMP_BNK
  69. End Proc
  70.      
  71. <CUT>
  72.      
  73. Quick tech explanation (hmmm, how it works, eh):
  74. I use the internal AMOS sort cmd. But before sorting I add the hex number to 
  75. the end of the strings. So if the strings are:
  76.      
  77. Hi
  78. There
  79. !!!
  80.      
  81. then I add like this:
  82.      
  83. Hi$0000
  84. There$0001
  85. !!!$0002
  86.      
  87. After sorting (using "Sort") it is:
  88.      
  89. !!!$0002
  90. Hi$0000
  91. There$0001
  92.      
  93. - and I can use the numbers to move around in the bank.
  94.      
  95. I reserve a temp bank on the size of one block (that is 256k)
  96.      
  97. As I do not have 32MB of ram I have not been able to test it with your amount 
  98. of memory. It moves the memory three times. That is: It will move 96MB of 
  99. data! So it'll probably use a small amount of time.
  100.      
  101. Just write a letter to the list (send a copy to me personally, as I am only 
  102. very seldom at home and therefore incidentally skips some letters in the 
  103. mailinglists) if you have questions.
  104.      
  105. -- 
  106.      
  107.          /?\ __    __ /?????\           _         Rune Zedeler
  108. ________/ /// \\__/ \\\  ---/           \?-_      Peter Rordams Vej 19 
  109. \      / //?|  \\/  ||?\ \\??????????????   ?-_   2800 Lyngby
  110.  )    / //  | \ ` / ||  \ \\ Lemmus of Efreet  -  Denmark
  111. /    / ?????\\|\-'/ /????? \\____________   _-?
  112. ?????\------'/||??| \------'/           /_-?      rzedeler@post10.tele.dk
  113.       ??????\-'/  \-'/??????            ?         Tel: +45-45871730
  114.              ??    ??
  115.      
  116.  
  117.